package com.wquick.base.baseView

import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.mutableLongStateOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.dp
import io.modifier.basic.compose.debouncedClickable
import io.modifier.basic.compose.theme.AppTheme
import io.modifier.basic.compose.theme.AppThemeHolder
import io.modifier.basic.compose.theme.size


abstract class BaseComposeToolBarFgt : BaseComposeFragment() {

    open val centerTitle: Boolean = AppThemeHolder.centerTitle

    open val blackTint: Boolean = AppThemeHolder.blackTint

    open val showBack: Boolean = true

    open val hideToolbar: Boolean = false

    open val leftRes: Int = 0

    open val title: String = ""

    open val rightTxt: String = ""

    open val showRightImg: Boolean = false

    open val rightRes: Int = 0

    @Composable
    open fun getRightColor(): Color {
        return AppThemeHolder.colors.appColor
    }

    open val rightClick: () -> Unit = {}

    protected val rightCanClickState by lazy { mutableStateOf(false) }

    protected val resetStatusBarMsState = mutableLongStateOf(0L)

    protected open var statusBarColorByCompose = Color.Transparent

    protected open var statusBarDarkIconByCompose = false

    @Composable
    override fun SetComposeRoot() {

        InjectTheme {
            SetStatusBar(
                window = requireActivity().window,
                color = statusBarColorByCompose,
                darkIcon = statusBarDarkIconByCompose,
                avoidStatusBar = true,
                resetStatusBarMs = resetStatusBarMsState.longValue,
                onResetStatusBarMsChange = {
                    resetStatusBarMsState.longValue = it
                }) {
                Column(Modifier.fillMaxSize()) {

                    if (hideToolbar.not())
                        ComposeToolBar(
                            showBack = showBack,
                            blackTint = blackTint,
                            leftRes = leftRes,
                            leftClick = {
                                if (!onBackPressedSupport()) atySupportBack()
                            },
                            titleColor = AppThemeHolder.colors.txtColor,
                            title, centerTitle, rightTxt, rightCanClickState,
                            @Composable {
                                getRightColor()
                            }, rightClick, showRightImg, rightRes
                        )

                    Box(modifier = Modifier.fillMaxSize()) {
                        Render()
                    }

                }
            }

            FullScreenBottomSheet()
        }
    }

    @Composable
    override fun InjectTheme(content: @Composable () -> Unit) {
        AppTheme() {
            content()
        }
    }

    @Composable
    open fun FullScreenBottomSheet() {

    }


}

@Composable
fun ComposeToolBar(
    showBack: Boolean = true,
    blackTint: Boolean = AppThemeHolder.blackTint,
    leftRes: Int = 0,
    leftClick: () -> Unit,
    titleColor: Color  = AppThemeHolder.colors.txtColor,
    title: String,
    centerTitle: Boolean = AppThemeHolder.centerTitle,
    rightTxt: String = "",
    rightCanClickState: MutableState<Boolean> = mutableStateOf(false),
    getRightColor: @Composable () -> Color = { Color.Transparent },
    rightClick: () -> Unit = {},
    showRightImg: Boolean = false,
    rightRes: Int = 0,
    modifier: Modifier = Modifier,
) {
    Box(
        modifier
            .height(48.dp)
            .fillMaxWidth(),
    ) {
        if (showBack) Image(
            painter = painterResource(
                id = if (leftRes != 0) leftRes
                else if (blackTint) com.wquick.base.R.drawable.svg_return_black
                else com.wquick.base.R.drawable.svg_return_white
            ),
            contentDescription = null,
            contentScale = ContentScale.Inside,
            modifier = Modifier
                .align(Alignment.CenterStart)
                .padding(start = 15.dp)
                .size(34.dp)
                .padding(5.dp)
                .clip(CircleShape)
                .debouncedClickable {
                    leftClick.invoke()
                }
        )

        Text(
            text = title,
            style = AppThemeHolder.textStyle.default.size(18).copy(color = if (blackTint) AppThemeHolder.colors.txtColor else titleColor),
            modifier = Modifier
                .padding(start = if (centerTitle) 0.dp else 49.dp)
                .align(if (centerTitle) Alignment.Center else Alignment.CenterStart)
        )

        if (rightTxt.isNotBlank() && rightCanClickState.value) {
            Text(
                text = "   $rightTxt   ",
                style = AppThemeHolder.textStyle.default.size(18).copy(
                    color = if (rightCanClickState.value) getRightColor()
                    else if (blackTint) AppThemeHolder.colors.txtColor else AppThemeHolder.colors.onAppColor
                ),
                modifier = Modifier
                    .align(Alignment.CenterEnd)
                    .wrapContentWidth()
                    .clip(RoundedCornerShape(6.dp))
                    .debouncedClickable {
                        rightClick.invoke()
                    })
        }


        if (showRightImg) Image(
            painter = painterResource(rightRes),
            contentDescription = null,
            modifier = Modifier
                .align(Alignment.CenterEnd)
                .padding(end = 15.dp)
                .size(24.dp)
                .clip(CircleShape)
                .debouncedClickable {
                    rightClick.invoke()
                },
            contentScale = ContentScale.Inside
        )

    }
}